package com.hollycrm.unicom.code.web;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;

import org.apache.commons.beanutils.PropertyUtils;

import com.hollycrm.springframework.factory.DefaultBeanFactory;
import com.hollycrm.unicom.code.CodeManager;
import com.hollycrm.unicom.code.GeneralCode;

/**
 * GeneralCodeLabelTag
*
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: </p>
 * @author Tian Kai
 * @version 1.0
 */

public class GeneralCodeLabelTag extends BodyTagSupport {

    private Object nameBean;
    private String name;
    private String codeType;
    private String codeKey;
    private String codeKeyProperty;

    public int doStartTag() throws JspException {

        GeneralCode generalCode = null;
        CodeManager codeManager = (CodeManager) DefaultBeanFactory.getBean(CodeManager.BEAN_ID);

        if (codeType == null) {
            if (name == null) throw new JspException("not find \"name\" property.");
            String codeTypeAndKey = getNameBeanPropertyValue();
            if (codeTypeAndKey == null) return (SKIP_BODY);
            int boundary_index = codeTypeAndKey.indexOf("/");
            if (boundary_index < 0 || boundary_index + 1 > codeTypeAndKey.length())
                throw new JspException("Illegal Argument, Please read tld.");
            codeType = codeTypeAndKey.substring(0, boundary_index);
            codeKey = codeTypeAndKey.substring(boundary_index + 1);
        }
        else if (codeKey == null) {
            codeKey = getNameBeanPropertyValue();
        }

        generalCode = codeManager.getGeneralCode(codeType, codeKey);

        if (generalCode != null) {
            try {
                pageContext.getOut().print(generalCode.getCodeName() == null ?
                                             "" : generalCode.getCodeName());
            }
            catch (IOException ex1) {
                throw new JspException(ex1);
            }
        }

        return (SKIP_BODY);
    }

    private String getNameBeanPropertyValue() throws JspException {
        String value = null;
        nameBean = pageContext.findAttribute(name);
        if (nameBean == null) throw new JspException("not find bean: " + name + " in any scope.");
        try {
            if (nameBean instanceof String)
                value = (String) nameBean;
            else
                value = (String)
                    PropertyUtils.getProperty(nameBean, codeKeyProperty);
        }
        catch (Exception ex) {
            throw new JspException("tag codeKeyProperty: " +
                                   codeKeyProperty + ex.getMessage());
        }
        return value;
    }

    public void release() {
        super.release();
        nameBean = null;
        name = null;
        codeType = null;
        codeKey = null;
        codeKeyProperty = null;
    }

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getCodeType() {
        return codeType;
    }
    public void setCodeType(String codeType) {
        this.codeType = codeType;
    }
    public String getCodeKey() {
        return codeKey;
    }
    public void setCodeKey(String codeKey) {
        this.codeKey = codeKey;
    }
    public String getCodeKeyProperty() {
        return codeKeyProperty;
    }
    public void setCodeKeyProperty(String codeKeyProperty) {
        this.codeKeyProperty = codeKeyProperty;
    }
    public Object getNameBean() {
        return nameBean;
    }
    public void setNameBean(Object nameBean) {
        this.nameBean = nameBean;
    }

}
